//
//  KongWIllnessViewController.m
//  LoveOfPet
//
//  Created by dllo on 15/11/17.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "KongWIllnessViewController.h"
#import "KongWURLSessionHandle.h"
#import "KongWCteatChartView.h"
#import "KongWIllnessDetailViewController.h"
#import "KongWDogWebViewController.h"
#import <SVProgressHUD.h>
#define kHeight     [UIScreen mainScreen].bounds.size.height / 667
#define kWidth      [UIScreen mainScreen].bounds.size.width / 375
@interface KongWIllnessViewController ()<ChartDelegate>

@property (retain, nonatomic) NSMutableArray *dogArray;

@property (retain, nonatomic) NSMutableArray *catArray;

@end

@implementation KongWIllnessViewController
-(void)dealloc
{
#warning 问题
    [_dogArray release];
    [_catArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 30, 30);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"KongWback"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.title = @"查疾病";
    self.dogArray = [NSMutableArray array];
    self.catArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadData];

}
-(void)loadData
{
    [KongWURLSessionHandle handleGETWithUrlString:@"http://app.5ichong.com/app.php/tag/tags" parameters:nil showHuD:YES onView:self.view successfulBlock:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *array = [dic objectForKey:@"data"];
        for (int i = 0; i < array.count; i ++) {
            if (i < 8) {
                [self.dogArray addObject:[array objectAtIndex:i]];
            }else
            {
                [self.catArray addObject:[array objectAtIndex:i]];
            }
        }
        
        [self creatChartView];
    } failureBlock:^(NSError *error) {
        NSLog(@"Error:%@", error);
    }];
}

-(void)creatChartView
{
    
    UIView *dogView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, kHeight * 80)];
    [self.view addSubview:dogView];
    [dogView release];
    UIImageView *dogImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"KongWiconfont-6"]];
    dogImg.frame = CGRectMake(kWidth * 80, kHeight * 25, kWidth * 40, kHeight * 40);
    [dogView addSubview:dogImg];
    [dogImg release];
    
    UILabel *dogTitle = [[UILabel alloc] initWithFrame:CGRectMake(dogImg.frame.origin.x + dogImg.frame.size.width + 40, dogImg.frame.origin.y, kWidth * 150, dogImg.frame.size.height)];
    dogTitle.text = @"宠物狗";
    dogTitle.font = [UIFont fontWithName:@"Helvetica-Bold" size:22];
    dogTitle.textColor = [UIColor colorWithRed:0.0 green:0.502 blue:1.0 alpha:1.0];
    [dogView addSubview:dogTitle];
    [dogTitle release];
    
    
    KongWCteatChartView *dogChart = [[KongWCteatChartView alloc] initWithFrame:CGRectMake(0, kHeight * 144, self.view.frame.size.width, kHeight * 184) withArray:self.dogArray andTager:10100];
    dogChart.delegate = self;
    [self.view addSubview:dogChart];
    [dogChart release];
    
    
    UIView *catView = [[UIView alloc] initWithFrame:CGRectMake(0, dogChart.frame.origin.y + dogChart.frame.size.height, self.view.frame.size.width, kHeight * 80)];
    [self.view addSubview:catView];
    [catView release];
    UIImageView *catImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"KongWiconfont-1"]];
    catImg.frame = CGRectMake(kWidth * 80, kHeight * 25, kWidth * 40, kHeight * 40);
    [catView addSubview:catImg];
    [catImg release];
    UILabel *catTitle = [[UILabel alloc] initWithFrame:CGRectMake(catImg.frame.origin.x + catImg.frame.size.width + 40, catImg.frame.origin.y, kWidth * 150, catImg.frame.size.height)];
    catTitle.text = @"宠物猫";
    catTitle.font = [UIFont fontWithName:@"Helvetica-Bold" size:22];
    catTitle.textColor = [UIColor colorWithRed:0.0 green:0.502 blue:1.0 alpha:1.0];
    [catView addSubview:catTitle];
    [catTitle release];
    KongWCteatChartView *catChart = [[KongWCteatChartView alloc] initWithFrame:CGRectMake(0, dogChart.frame.origin.y + dogChart.frame.size.height + 80, self.view.frame.size.width, dogChart.frame.size.height) withArray:self.catArray andTager:10000];
    catChart.delegate = self;
    [self.view addSubview:catChart];
    [catChart release];
}

-(void)returnUdi:(NSString *)uid andTitle:(NSString *)title
{
    KongWIllnessDetailViewController *illnessDetail = [[KongWIllnessDetailViewController alloc] init];
    illnessDetail.uid = uid;
    illnessDetail.titleStr = title;
    illnessDetail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:illnessDetail animated:YES];
    [illnessDetail release];
}

-(void)returnTager:(NSInteger)btnTager
{
    if (btnTager == 10108) {
        KongWDogWebViewController *dogWeb = [[KongWDogWebViewController alloc] init];
        dogWeb.taget = 1;
        dogWeb.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:dogWeb animated:YES];
        [dogWeb release];
    }else
    {
        KongWDogWebViewController *catWeb = [[KongWDogWebViewController alloc] init];
        catWeb.taget = 2;
        catWeb.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:catWeb animated:YES];
        [catWeb release];
    }
}
-(void)backBtnAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillDisappear:(BOOL)animated{
    [SVProgressHUD dismiss];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
